<extend name="Public:base" />

<block name="title">登陆 - 后台</block>

<block name="body">
    <div class="container">
    	<div class="col-md-offset-4 col-md-4">
	      <form class="form-signin" role="form">
	        <h2 class="form-signin-heading text-center">买买网后台</h2>
	        <br />
	        <input type="text" id="name" class="form-control" placeholder="管理员账号" required autofocus>
	        <input type="password" id="password" class="form-control" placeholder="密码" required>
	        <br />
			<div class="form-group">
	        	<button type="button" id="submitBtn" class="btn btn-primary btn-block">登陆到后台</button>
	        </div>
	      </form>
	      <div id="errPanel" class="alert alert-danger text-center hidden" role="alert"></div>
		</div>
    </div> <!-- /container -->
</block>

<block name="js">
	<script type="text/javascript" charset="utf-8">
	$(function() {
		$("#submitBtn").click(function() {
			// 先禁用
			$("#submitBtn").attr("disabled", "disabled")
			$("#submitBtn").html("登陆中。。。")
			
			// 获取数据
			var args = {
				"name":		$("#name").val(),
				"password":	$("#password").val(),
			}
		
			// 校验
			if (args.name == "") {
				return showErr("管理员账号不能为空")
			}
			if (args.password == "") {
				return showErr("密码不能为空")
			}

			// 联网检验
			$.post("{:U('Api/AdminSign/postSignIn')}", args, function(data) {
				console.log(data)
				
				// 登陆失败
				if (data.status != 200) {
					return showErr(data.msg)
				}
				
				// 登陆成功
				localStorage.setItem("SESSID", data.sessId)
				localStorage.setItem("adminName", args.name)
				location = "{:U('Admin/main')}"
				
			}, "json")
			
		});
	});
	
	/**
	 * 显示错误
	 */
	function showErr(err) {
		$("#errPanel").html(err)
		$("#errPanel").removeClass("hidden")
		// 恢复被禁用
		$("#submitBtn").removeAttr("disabled")
		$("#submitBtn").html("登陆到后台")
	}
	</script>
</block>
